<template>
	<view>
		<!-- <u-notice-bar v-if="mySetmeal.enable_video_interview===0" :scrollable="false" text="当前套餐不允许使用视频面试，请发起普通面试邀请" /> -->
		<Head title="面试邀请" type="public" :isBack="false"></Head>
		<u-form ref="invitationForm">
			<view class="invitaion_box">
				<u-field required label="面试形式">
					<u-radio-group slot="right" v-model="form.type" style="flex: 5;" icon-size="28rpx">
						<u-radio name="1">面试邀请</u-radio>
						<!-- <u-radio name="2" :disabled="mySetmeal.enable_video_interview===0?true:false">视频面试</u-radio> -->
					</u-radio-group>
				</u-field>
			</view>
			<u-field placeholder-style="color:#757575;font-size:14px !important;" required v-model="apply_fullname"
				label="面试人" placeholder="请填写" class="reset_after" />
			<view v-if="from != 'apply'" class="form-line u-border-bottom" @click="showJobsPicker = true">
				<view class="form-field">
					<view class="label">
						面试职位
					</view>
					<view :class="jobname  ? 'text' : 'text ac'">
						{{jobname ? jobname : '请选择'}}
					</view>
				</view>
				<u-icon class="arrow-icon" name="arrow-right"></u-icon>
			</view>

			<u-picker v-if="from != 'apply'" v-model="showJobsPicker" mode="selector" :range="options_jobs"
				range-key="jobname" @confirm="onConfirmJob" @cancel="showJobsPicker = false" />
			<view class="form-line u-border-bottom" @click="showDatePicker = true">
				<view class="form-field">
					<view class="label">
						面试日期
					</view>
					<view :class="form.interview_date ? 'text' : 'text ac'">
						{{form.interview_date ? form.interview_date : '请选择'}}
					</view>
				</view>
				<u-icon class="arrow-icon" name="arrow-right"></u-icon>
			</view>
			<!-- <u-picker v-model="showDatePicker" mode="time"  :start-year="dateYear" :default-time="minDate" @confirm="handlerDate" title="请选择面试日期"/> -->
			<u-select v-model="showDatePicker" mode="mutil-column-auto" :list="invitationList2"
				@confirm="onConfirmWage3" title="请选择面试日期"></u-select>
			<view class="form-line u-border-bottom" @click="showTimePicker = true">
				<view class="form-field">
					<view class="label">
						面试时间
					</view>
					<view :class="form.interview_time ? 'text' : 'text ac'">
						{{form.interview_time ? form.interview_time : '请选择'}}
					</view>
				</view>
				<u-icon class="arrow-icon" name="arrow-right"></u-icon>
			</view>
			<u-select v-model="showTimePicker" mode="mutil-column" title="请选择面试时间" :list="invitationList"
				@confirm="handlerTime"></u-select>
			<u-field placeholder-style="color:#757575;font-size:14px !important;" required v-show="form.type==1"
				v-model="form.address" label="面试地点" placeholder="请填写" class="reset_after"
				:rules="[{ required: true, message: '请填写面试地点' }]" />
			<view class="form_split_10"></view>
			<u-field placeholder-style="color:#757575;font-size:14px !important;" required v-model="form.contact"
				label="联系人" placeholder="请填写" class="reset_after" :rules="[{ required: true, message: '请填写联系人' }]" />
			<u-field placeholder-style="color:#757575;font-size:14px !important;" required v-model="form.tel"
				type="number" label="联系电话" placeholder="请填写" class="reset_after"
				:rules="[{ required: true, message: '请填写联系电话' }]" />
			<u-field v-model="form.note" label="备注" placeholder="请填写" class="reset_after" />
			<view style="margin: 32rpx 32rpx 20rpx;">
				<u-button shape="circle" type="primary" @click="onSubmit">
					发送面试邀请
				</u-button>
			</view>
			<view style="margin: 0 32rpx;">
				<u-button class="cancelBtn" shape="circle" type="default" @click="back()">
					取消
				</u-button>
			</view>
		</u-form>
		<u-top-tips ref="uTips"></u-top-tips>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import http from '@/utils/http'
	import api from '@/api'
	export default {
		name: 'AddInvitation',
		props: [
			'from',
			'apply_id',
			'apply_dataset_index',
			'apply_fullname',
			'resume_id'
		],
		data() {
			return {
				interviewParams: {
					year: false,
					month: false,
					day: false,
					hour: true,
					minute: true,
					second: false
				},
				dateYear: new Date().getFullYear(),
				minDate: '',
				showJobsPicker: false,
				showDatePicker: false,
				showTimePicker: false,
				jobname: '',
				form: {
					type: '1',
					jobid: '',
					interview_date: '',
					interview_time: '',
					address: '',
					contact: '',
					tel: '',
					note: ''
				},
				isFilterTime: false,
				// joblist: [],
				options_jobs: [],
				mySetmeal: {}
			}
		},
		computed: {
			invitationList() {
				return this.dataList()
			},
			invitationList2() {
				return this.date3()
			},
		},
		mounted() {
			// this.initCB()
			this.minDate = this.dateFormat(new Date(new Date().getTime()), 'yyyy-MM-dd')
		},
		watch: {
			'form.interview_date': function(value) {
				this.$nextTick(function() {
					let myDate = new Date() // 当前日期
					let year = myDate.getFullYear() // 获取当前年
					let mon = myDate.getMonth() + 1 // 获取当前月
					let date = myDate.getDate() // 获取当前日
					let chooseDate = value.split('-')
					this.isFilterTime = year === parseInt(chooseDate[0]) && mon === parseInt(chooseDate[1]) &&
						date === parseInt(chooseDate[2])
					this.filterTime()
					if (this.isFilterTime) {
						this.form.interview_time = ''
					}
				})
			},
		},
		methods: {
			dataList() {
				var d = new Date();
				var val = parseInt(this.form.interview_date.split('-')[1])
				var val2 = parseInt(this.form.interview_date.split('-')[2])
				if (val == d.getMonth() + 1 && val2 == d.getDate()) {
					var currentHour = d.getHours() + 1
				} else {
					var currentHour = 8
				}
				var maxHour = 20
				var hourAry = []
				var branchAry = [{
						value: '00分',
						label: '00分'
					},
					{
						value: '10分',
						label: '10分'
					},
					{
						value: '20分',
						label: '20分'
					},
					{
						value: '30分',
						label: '30分'
					},
					{
						value: '40分',
						label: '40分'
					},
					{
						value: '50分',
						label: '50分'
					}
				]
				for (var i = currentHour; i <= maxHour; i++) {
					hourAry.push({
						value: i + '时',
						label: i + '时'
					})
				}
				return [hourAry, branchAry]
			},
			date3() {
				const nowDate = new Date()
				const nowYear = nowDate.getFullYear()
				const month = nowDate.getMonth() + 1;
				const curDay = nowDate.getDate();
				const minYear = nowYear - 59
				const yearArr = []
				const monthArr = []
				const arr = []
				var day = 0
				// 生成年份
				for (let i = 0; i < nowYear - minYear; i++) {
					yearArr.push((nowYear - i) + '年')
				}
				//生成月份
				for (let j = 0; j < 12; j++) {
					monthArr.push({
						value: (j + 1) + '月',
						label: (j + 1) + '月',
						children: []
					})
				}
				// 获取每月总数（每个月多少天）
				for (let k = 0; k < yearArr.length; k++) {
					let yearNewArr = []
					for (let m = 0; m < monthArr.length; m++) {
						day = new Date(parseInt(yearArr[k]), parseInt(monthArr[m].value), 0).getDate()
						let dayArry = []
						for (let d = 0; d < day; d++) {
							dayArry.push({
								label: (d + 1) + '日',
								value: (d + 1) + '日',
							})
						}
						yearNewArr.push({
							label: monthArr[m].label,
							value: monthArr[m].value,
							children: dayArry
						})
					}
					// 最后生成结果
					arr.push({
						label: yearArr[k],
						value: yearArr[k],
						children: yearNewArr
					})
				}
				arr[0].children = arr[0].children.slice((month - 1))
				arr[0].children[0].children = arr[0].children[0].children.slice(curDay - 1)
				return arr
			},
			onConfirmWage3(values) {
				var y = parseInt(values[0].label)
				var m = parseInt(values[1].label) >= 10 ? parseInt(values[1].label) : '0' + parseInt(values[1].label)
				var d = parseInt(values[2].label) >= 10 ? parseInt(values[2].label) : '0' + parseInt(values[2].label)
				this.form.interview_date = y + '-' + m + '-' + d
			},
			dateFormat(date, format) {
				format = format || 'yyyy-MM-dd hh:mm:ss';
				if (date !== 'Invalid Date') {
					let o = {
						"M+": date.getMonth() + 1, //month
						"d+": date.getDate(), //day
						"h+": date.getHours(), //hour
						"m+": date.getMinutes(), //minute
						"s+": date.getSeconds(), //second
						"q+": Math.floor((date.getMonth() + 3) / 3), //quarter
						"S": date.getMilliseconds() //millisecond
					}
					if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
						(date.getFullYear() + "").substr(4 - RegExp.$1.length));
					for (let k in o)
						if (new RegExp("(" + k + ")").test(format))
							format = format.replace(RegExp.$1,
								RegExp.$1.length === 1 ? o[k] :
								("00" + o[k]).substr(("" + o[k]).length));
					return format;
				}
				return '';

			},
			// 初始化
			initCB() {
				this.jobname = ''
				this.form = {
					type: '1',
					jobid: '',
					interview_date: '',
					interview_time: '',
					address: '',
					contact: '',
					tel: '',
					note: ''
				}
				if (this.from !== 'apply') {
					http
						.get(api.company_published_jobslist, {})
						.then(res => {
							this.options_jobs = res.data.data
							// res.data.data.forEach(element => {
							// 	this.joblist.push(element)
							// })
						})
						.catch(() => {})
				}
				// 获取面试地点以及联系人
				this.fetchSetmeal()
				this.fetchIterviewPre()
			},
			// 获取联系人
			fetchSetmeal() {
				http
					.get(api.member_setmeal, {})
					.then(res => {
						this.mySetmeal = res.data.data.info
					})
					.catch(() => {})
			},
			// 获取地点
			fetchIterviewPre: function() {
				http
					.get(api.interview_add_pre, {})
					.then(res => {
						this.form.address = res.data.data.address
						this.form.contact = res.data.data.contact
						this.form.tel = res.data.data.tel
					})
					.catch(() => {})
			},
			back() {
				//关闭面试邀请
				uni.navigateBack({
					delta: 1
				})
				this.$emit('closePopup')
			},
			// 提交保存
			onSubmit(values) {
				if (this.from === 'apply') {
					if (this.form.interview_date == '') {
						this.$refs.uToast.show({
							title: '请选择面试日期'
						})
						return
					}
					if (this.form.interview_time == '') {
						this.$refs.uToast.show({
							title: '请选择面试时间'
						})
						return
					}
					if (this.form.address == '') {
						this.$refs.uToast.show({
							title: '请填写面试地点'
						})
						return
					}
					if (this.form.contact == '') {
						this.$refs.uToast.show({
							title: '请填写联系人'
						})
						return
					}
					if (this.form.tel == '') {
						this.$refs.uToast.show({
							title: '请填写联系电话'
						})
						return
					}
					this.form.apply_id = this.apply_id
					http
						.post(api.company_jobapply_set_agree, this.form)
						.then(res => {
							if (res.data.code == 200) {
								uni.showToast({
									title: res.data.message,
									icon: 'none'
								})
								setTimeout(() => {
									uni.navigateBack({
										delta: 1
									})
								}, 2000)
							}
						})
						.catch(() => {})
				} else {
					if (this.form.jobid == '') {
						this.$refs.uToast.show({
							title: '请选择面试职位'
						})
						return
					}
					if (this.form.interview_date == '') {
						this.$refs.uToast.show({
							title: '请选择面试日期'
						})
						return
					}
					if (this.form.interview_time == '') {
						this.$refs.uToast.show({
							title: '请选择面试时间'
						})
						return
					}
					if (this.form.address == '') {
						this.$refs.uToast.show({
							title: '请填写面试地点'
						})
						return
					}
					if (this.form.contact == '') {
						this.$refs.uToast.show({
							title: '请填写联系人'
						})
						return
					}
					if (this.form.tel == '') {
						this.$refs.uToast.show({
							title: '请填写联系电话'
						})
						return
					}
					this.form.resume_id = this.resume_id
					this.form.jobid = this.form.jobid
					http
						.post(api.interview_add, this.form)
						.then(res => {
							if (res.data.code == 200) {
								uni.showToast({
									title: res.data.message,
									icon: 'none'
								})
								setTimeout(() => {
									uni.navigateBack({
										delta: 1
									})
								}, 1500)
							}
						})
						.catch(() => {})
				}
			},
			//面试时间
			handlerTime(time) {
				var hour = parseInt(time[0].label)
				var minute = parseInt(time[1].label)
				if (minute == 0) {
					minute = '00'
				}
				this.form.interview_time = hour + ':' + minute
				this.showTimePicker = false
			},
			filterTime(type, options) {
				if (this.isFilterTime) {
					if (type === 'hour') {
						return options.filter(option => option > new Date().getHours())
					}
				}
				if (type === 'minute') {
					return options.filter(option => option % 10 === 0)
				}
				return options
			},
			onConfirmJob(index) {
				this.jobname = this.options_jobs[index].jobname
				this.form.jobid = this.options_jobs[index].id
				// for (const key in this.joblist) {
				// 	if (key == index) {
				// 		this.form.jobid = this.joblist[key].id
				// 		break
				// 	}
				// }
				this.showJobsPicker = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.invitaion_box {
		/deep/ .fild-body {
			/deep/ .u-flex {
				flex: 0;
			}
		}
	}

	.split_bar {
		width: 100%;
		height: 18rpx;
	}

	input {
		-webkit-user-select: auto !important;
	}
</style>